# -*- coding:utf-8 -*-

import datetime
import os
import time
import xlrd
from pywinauto.application import Application
import pyautogui
import pyperclip
pyautogui.FAILSAFE = False
pyautogui.PAUSE = 0.3
import threading


class Erpauto(threading.Thread):

    def __init__(self, workspace):
        super().__init__()

        self.erp_dir = os.path.join(workspace, 'erp')

        # self.ui=ui
        os.makedirs(self.erp_dir, exist_ok=True)
        # 打开大写
        pyautogui.press('capslock')

    def read(self, code):

        dir = self.erp_dir
        # ui = self.ui

        app = Application(backend="win32").connect(title_re="旺店通ERP")
        dlg_spec = app.window(title=u'旺店通ERP', visible_only=False)
        dlg_spec.restore()
        dlg_spec.move_window(x=0, y=0, width=1000, height=600)
        dlg_spec.set_focus()

        # 点击输入框删除
        pyautogui.click(666, 122)
        # 输入货品编号
        pyautogui.typewrite(code,interval=0.01)
        # 点击搜索
        pyautogui.click(720, 118)
        # 等待
        time.sleep(1)
        # 打开右键菜单
        pyautogui.rightClick(34, 161)
        # 等
        time.sleep(0.1)
        # 点击导出选项
        pyautogui.click(87, 205)
        # 点击导出按钮
        pyautogui.click(392,128)
        # 等待
        time.sleep(0.2)
        # 调整另存为窗口
        save_dlg_spec = app.window(title=u'另存为')
        save_dlg_spec.move_window(x=0, y=0, width=
            800, height=600)

        # 生成文件名
        now = datetime.datetime.now()
        tmp_file_name = now.strftime("%Y%m%d%H%M%S%f")
        tmp_file_path = os.path.join(dir, tmp_file_name+'.xlsx')
        print('输出文件名称路径：',tmp_file_path)
        # 复制文件名
        pyperclip.copy(tmp_file_path)
        # 粘贴地址
        pyautogui.hotkey('ctrl', 'v')
        time.sleep(2)
        # # 保存
        pyautogui.hotkey('alt', 's')
        # 等待
        time.sleep(1)
        # # 点击不立即打开文件
        # pyautogui.click(round(1000*scale),round(600*scale))
        # 右键
        pyautogui.press('right')
        # 回车

        pyautogui.press('enter')
        # 缩小窗口
        # dlg_spec.minimize()


        excel_file_name = os.path.join(dir, tmp_file_name+'.xlsx')

        # 等待文件写入完成
        while not os.path.exists(excel_file_name):
            print('等待导出完成')
            time.sleep(6)

        print('Reading Excel file: {}'.format(excel_file_name))

        # pyautogui.hotkey('alt', 'tab')
        # pyautogui.hotkey('win', '2')

        time.sleep(0.5)
        # 点击不立即打开文件
        pyautogui.click(1000,600)

        # 读取excel文件
        # excel_file_name='20200911105922509319.xlsx'
        data = xlrd.open_workbook(excel_file_name)
        good_list = []
        table = data.sheets()[0]
        color = []
        size = []
        yanse = []
        chima = []
        if table.nrows > 2:
            for i in range(table.ncols):
                if table.col_values(i)[0] == '货品名称':
                    spec = table.col_values(i)[1]
                elif table.col_values(i)[0] == '货品编号':
                    good = table.col_values(i)[1]
                elif table.col_values(i)[0] == '规格名称':
                    for g in table.col_values(i)[1:]:
                        yanse.append(g.replace("  "," ").split(" ")[0])
                        chima.append(g.replace("  "," ").split(" ")[1])
            color = list(set(yanse))
            size = list(set(chima))
            size.sort(key=chima.index)
            # print(color)
            # print(size)
            good_list.append(good)
            good_list.append(spec)
            good_list.append(color)
            good_list.append(size)
            print(good_list)
            return good_list
        else:
            print('未获取到该货号的数据')






if __name__=='__main__':
    erp=Erpauto('E:\\')
    erp.start()
    erp.read('ma032551-1')

